package com.atguigu.config;


import org.apache.http.HttpHost;
import org.apache.http.protocol.HTTP;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author gjl
 * @create 2021-03-17 18:37
 */
@Configuration
@ConfigurationProperties(prefix = "elasticsearch")
public class ElasticSearchConfig {
    private String host;
    private int port;

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    @Override
    public String toString() {
        return "ElasticSearchConfig{" +
                "host='" + host + '\'' +
                ", port=" + port +
                '}';
    }

    @Bean
    public RestHighLevelClient client() {

        HttpHost httpHost = new HttpHost(host, port, "http");

        RestClientBuilder builder = RestClient.builder(httpHost);

        RestHighLevelClient client = new RestHighLevelClient(builder);

        return client;
        //return new RestHighLevelClient(RestClient.builder(new HttpHost(host,port,"http")));
    }

}
